package cn.org.xwb.bookmall.model.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.org.xwb.bookmall.model.constant.ManagerConst;
import cn.org.xwb.bookmall.model.po.Authority;
import cn.org.xwb.bookmall.model.mapper.AuthorityMapper;
import cn.org.xwb.bookmall.model.po.Manager;
import cn.org.xwb.bookmall.model.service.IAuthorityService;
import cn.org.xwb.bookmall.model.utils.BookRedisUtil;
import cn.org.xwb.common.page.dto.PageResult;
import cn.org.xwb.common.page.utils.PageUtil;
import cn.org.xwb.common.redis.utils.RedisUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author xwb
 * @since 2023-11-25
 */
@Service
public class AuthorityServiceImpl extends ServiceImpl<AuthorityMapper, Authority> implements IAuthorityService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private BookRedisUtil bookRedisUtil;

    /**
     * 获取 redis 中存储 authority 的 key
     *
     * @return
     */
    private String getKeyAuthority() {
        return bookRedisUtil.getRedisKeyAdmin(ManagerConst.AUTHORITY, "");
    }

    /**
     * 获取所有权限组成的列表，并缓存
     *
     * @return null-发生异常
     */
    @Override
    public List<Authority> findAndCacheAll() {
        // 查询所有权限信息
        List<Authority> authorities = baseMapper.selectAll();
        // 缓存
        Boolean res = redisUtil.setList(getKeyAuthority(), authorities);
        if (!res) return null;
        return authorities;
    }

    /**
     * 从缓存中获取所有权限，如果缓存不存在，则从数据库中查询，并缓存
     *
     * @return null-发生异常
     */
    @Override
    public List<Authority> findAllFromCache() {
        // 查询缓存
        List<Authority> authorities = (List<Authority>) redisUtil.get(getKeyAuthority());
        // 缓存中不存在，则从数据库中查询，并缓存
        if (Objects.isNull(authorities)) authorities = findAndCacheAll();
        // 发生异常
        if (Objects.isNull(authorities)) return null;
        // 成功
        return authorities;
    }

    /**
     * 分页获取权限列表
     *
     * @param current
     * @param pageSize 每页数据个数
     * @param disabled
     * @param nameMatch
     * @param codeMatch
     * @return
     */
    @Override
    public PageResult<List<Authority>> findAuthorityPage(Integer current, Integer pageSize, Boolean disabled, String nameMatch, String codeMatch) {
        // 获取全部数据
        List<Authority> authorities = findAllFromCache();
        // 异常
        if (Objects.isNull(authorities)) return null;
        // 条件过滤
        if (Objects.nonNull(disabled))
            authorities = authorities.stream().filter(authority -> authority.getDisabled() == disabled).toList();
        if (Objects.nonNull(nameMatch) && !nameMatch.isEmpty())
            authorities = authorities.stream().filter(authority -> authority.getName().contains(nameMatch)).toList();
        if (Objects.nonNull(codeMatch) && !codeMatch.isEmpty())
            authorities = authorities.stream().filter(authority -> authority.getCode().contains(codeMatch)).toList();
        // 页数
        Integer pageCnt = PageUtil.getPageCnt(authorities.size(), pageSize);
        // 分页数据
        authorities = (List<Authority>) PageUtil.getPageData(authorities, current, pageSize);
        return new PageResult<>(current, pageSize, authorities.size(), pageCnt, authorities);
    }
}
